package net.framework.smc.kotlin.demokotlin.service

import net.framework.smc.kotlin.demokotlin.dao.TripRepository
import net.framework.smc.kotlin.demokotlin.po.ResultPo
import net.framework.smc.kotlin.demokotlin.po.TripPo
import org.jetbrains.annotations.NotNull
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.text.SimpleDateFormat
import java.util.*
import javax.transaction.Transactional

/**
 *  @authore zyx
 *  @date 2018/7/5 16:52
 */
@Service
class TripService{

    @Autowired
    lateinit var tripRepository: TripRepository
    @Autowired
    lateinit var sendService: SendService

    /**
     *  今日行程列表
     *  type 类型0
     *
     */
    fun listTrip(type: Int? = 0, @NotNull userId : String) : ResultPo {
        var simpleDateFormat = SimpleDateFormat("yyyy-MM-dd")
        var date = Date()
        var start = simpleDateFormat.format(date) + " 00:00:00"
        var end = simpleDateFormat.format(date) + " 23:59:59"
        var list = tripRepository.findByStatus(userId, type, start, end)
        return ResultPo.success(list)
    }

    /**
     *  删除行程
     */
    fun delTrip(@NotNull id : String) : ResultPo{
        var tripPo = tripRepository.findById(id).get()
        tripPo.delFlag = 1
        tripRepository.save(tripPo)
        return ResultPo()
    }

    /**
     * 添加行程
     */
    fun addTrip(tripPo: TripPo ) : ResultPo {
        tripRepository.save(tripPo)
        return ResultPo()
    }

    /**
     * 完成行程
     */
    @Transactional
    fun finishedTrip(id : String) : ResultPo {
        var tripPo = tripRepository.findById(id).get()
        tripPo.status = 1
        tripRepository.save(tripPo)
        var date = Date()
        var simplleDateFormat = SimpleDateFormat("yyyy-MM-dd  HH:mm:ss")
        tripPo.endTime = simplleDateFormat.format(date)
        sendService.send( "用户" + tripPo.owner +"于" + simplleDateFormat.format(date) +"完成行程！")
        return ResultPo()
    }
}



